package com.base.taskreports.reports.controller;

import com.base.taskreports.common.annotation.Resubmit;
import com.base.taskreports.common.annotation.TBatchLog;
import com.base.taskreports.common.utlis.DateUtils;
import com.base.taskreports.common.utlis.ExcelUtils;
import com.base.taskreports.common.utlis.FileUtils;
import com.base.taskreports.demoExel.entity.Indicator;
import com.base.taskreports.reports.service.ExcelBeanService;
import com.base.taskreports.task.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@ResponseBody
public class ExcelController {

    @Autowired
    private LjService ljService;

    @Autowired
    private PqlService pqlService;

    @Autowired
    private PqdService pqdService;

    @Autowired
    private JfsService jfsService;

    @Autowired
    private JfuService jfuService;

    @Autowired
    private ExcelBeanService excelBeanService;

    @Value("${excelUrl}")
    private String excelUrl;

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<List<Object>> list = null;
        /*System.out.println(list);
        System.out.println("LJ_M003".indexOf("LJ_M"));*/

        Method method = LjService.class.getMethod("get" + "LJ_M006" + "_Detail", String.class);
        list = (List<List<Object>>) method.invoke("2021-11-10");
    }

    @Resubmit(delaySeconds = 120)
    @RequestMapping("/createExcel")
    public String createExcel(String strDate) {

        if (strDate==null || !DateUtils.isStrDate(strDate)) {
            return "参数输入错误,strDate=2021-11-10";
        }

        int stratRow = 3;

        String fileDateTime = strDate.replace("-", "");

        String outPath = excelUrl + "report.xlsx";
        String newPath = excelUrl + fileDateTime + "_report.xlsx";

        List<List<Object>> list = null;
        Method method = null;

        try {
            FileUtils.deleteFile(newPath);
            FileUtils.copyFileUsingJava7Files(new File(outPath), new File(newPath));

            ExcelUtils.addHeaderDate(newPath, strDate); // 为所有 Sheet 表头加入日期

            for (Indicator indicator : Indicator.values()) {

                list = excelBeanService.getExcelList(strDate, indicator.toString());
                //System.out.println(list);
                ExcelUtils.addExcelSheetContent(newPath, indicator.getIndex(), stratRow, list);

                /*if (indicator.getFloatFlag()) {

                    if (indicator.getName().indexOf("LJ_M") == 0) {
                        method = LjService.class.getMethod("get" + indicator.getName() + "_Detail", String.class);
                        list = (List<List<Object>>) method.invoke(ljService, strDate);

                    } else if (indicator.getName().indexOf("PQL_M") == 0) {
                        method = PqlService.class.getMethod("get" + indicator.getName() + "_Detail", String.class);
                        list = (List<List<Object>>) method.invoke(pqlService, strDate);

                    } else if (indicator.getName().indexOf("PQD_M") == 0) {
                        method = PqdService.class.getMethod("get" + indicator.getName() + "_Detail", String.class);
                        list = (List<List<Object>>) method.invoke(pqdService, strDate);

                    } else if (indicator.getName().indexOf("JFS_M") == 0) {
                        method = JfsService.class.getMethod("get" + indicator.getName() + "_Detail", String.class);
                        list = (List<List<Object>>) method.invoke(jfsService, strDate);

                    } else if (indicator.getName().indexOf("JFU_M") == 0) {
                        method = JfuService.class.getMethod("get" + indicator.getName() + "_Detail", String.class);
                        list = (List<List<Object>>) method.invoke(jfuService, strDate);

                    }

                }*/
            }

            list = ljService.getLJ_M001_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M001").getIndex(), 10, list) ;

            list = ljService.getLJ_M006_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M006").getIndex(), 10, list) ;

            list = ljService.getLJ_M007_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M007").getIndex(), 10, list) ;

            list = ljService.getLJ_M008_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M008").getIndex(), 10, list) ;

            list = ljService.getLJ_M009_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M009").getIndex(), 10, list) ;

            list = ljService.getLJ_M010_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M010").getIndex(), 10, list) ;

            list = ljService.getLJ_M011_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M011").getIndex(), 10, list) ;

            list = ljService.getLJ_M012_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("LJ_M012").getIndex(), 10, list) ;

            list = pqdService.getPQD_M001_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M001").getIndex(), 10, list) ;

            list = pqdService.getPQD_M006_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M006").getIndex(), 10, list) ;

            list = pqdService.getPQD_M007_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M007").getIndex(), 10, list) ;

            list = pqdService.getPQD_M008_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M008").getIndex(), 10, list) ;

            list = pqdService.getPQD_M009_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M009").getIndex(), 10, list) ;

            list = pqdService.getPQD_M010_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M010").getIndex(), 10, list) ;

            list = pqdService.getPQD_M011_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M011").getIndex(), 10, list) ;

            list = pqdService.getPQD_M012_Detail(strDate) ;
            ExcelUtils.addExcelSheetContent(newPath, Indicator.valueOf("PQD_M012").getIndex(), 10, list) ;

            List<Object> objectList = null;

            //单用户货币积分余额> 	前50条写入报表
            List<Object> getJFS_M004_50 = jfsService.getJFS_M004_Detail(strDate);
            this.selectFiftyDaysAgo(getJFS_M004_50,objectList,newPath,10,Indicator.JFS_M004.getIndex(),DateUtils.transformatDate(strDate).getTime());

            // 单用户非货币积分余额> N浮动比率	前50条写入报表
            List<Object> getJFS_M005_50 = jfsService.getJFS_M005_Detail(strDate);
            this.selectFiftyDaysAgo(getJFS_M005_50,objectList,newPath,10,Indicator.JFS_M005.getIndex(),DateUtils.transformatDate(strDate).getTime());

            // 积分余额为负数量	前50条写入报表
            List<Object> getJFS_M007_50 = jfsService.getJFS_M007_Detail(strDate);
            this.selectFiftyDaysAgo(getJFS_M007_50,objectList,newPath,10,Indicator.JFS_M007.getIndex(),DateUtils.transformatDate(strDate).getTime());

            //积分兑换价值比不为1笔数 前50
            List<Object> getJFU_M007_50 = jfuService.getJFU_M007_Detail( strDate);
            this.selectFiftyDaysAgo(getJFU_M007_50,objectList,newPath,10,Indicator.JFU_M007.getIndex(),DateUtils.transformatDate(strDate).getTime());


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } /*catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }*/

        return strDate + " Excel文件生成成功！";
    }


    //积分 --查询前50天的
    public void  selectFiftyDaysAgo(List<Object> excelBeanList,List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
        List<List<Object>> list = new ArrayList<List<Object>>();
        list.add(excelBeanList);
        System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
        System.out.print(System.currentTimeMillis() - startTime);

    }
}
